package com.qrcode.zxing;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

public class ReadQRcode {

	public static void main(String[] args) {
		try {
			MultiFormatReader formatReader = new MultiFormatReader();
			File file = new File("E:/img.png");
			//将二维码图片读入缓冲区
			BufferedImage image = ImageIO.read(file);
			//转化为zxing的二进制位图
			BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
			//创建哈希图并设置编码格式
			HashMap hints = new HashMap();
			hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
			//执行解析(二进制位图,哈希图(存放编码格式))
			Result result = formatReader.decode(binaryBitmap,hints);
			//输出结果
			System.out.println("解析结果:"+result.toString());
			System.out.println("二维码格式类型:"+result.getBarcodeFormat());
			System.out.println("二维码文本内容:"+result.getText());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
